package com.aaa.sys.aop;



import com.aaa.sys.entity.OperationLog;
import com.aaa.sys.service.OperationLogService;
import com.aaa.util.JwtUtils;
import com.aaa.vo.R;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class OperationAop {

    @Autowired
    private OperationLogService logService;

    @Pointcut("execution(* com.aaa.sys.controller..*.*(..))")
    public void exceptionLogPoinCut() {
    }

    @Pointcut("execution(* com.aaa.sys.controller.*.add*(..))")
    public void addCell() {}

    @Pointcut("execution(* com.aaa.sys.controller.*.delete*(..))")
    public void deleteCell() {}

    @Pointcut("execution(* com.aaa.sys.controller.*.update*(..))")
    public void updateCell() {}

    @Pointcut("execution(* com.aaa.sys.controller.*.fenUserRole*(..))")
    public void fenUserRole() {}

    @Pointcut("execution(* com.aaa.sys.controller.*.addRolePermission*(..))")
    public void addRolePermission() {}

    /*添加操作*/
    @AfterReturning(value = "addCell()", returning = "object")
    public void insertLog(JoinPoint joinPoint, Object object) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        //R r= (R) object;
        OperationLog operationLog=new OperationLog();
        operationLog.setLogType("业务日志");
        operationLog.setUserId(userid);
        operationLog.setLogName("添加操作");
        operationLog.setClassName(joinPoint.getTarget().getClass().getName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setCreateTime(new Date());
        //operationLog.setSucceed(r.getMessage());
        operationLog.setMessage("message");
        logService.save(operationLog);
    }
    /*删除操作*/
    @AfterReturning(value = "deleteCell()", returning = "object")
    public void deleteCell(JoinPoint joinPoint, Object object) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        //R r= (R) object;
        OperationLog operationLog=new OperationLog();
        operationLog.setLogType("业务日志");
        operationLog.setUserId(userid);
        operationLog.setLogName("删除操作");
        operationLog.setClassName(joinPoint.getTarget().getClass().getName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setCreateTime(new Date());
        //operationLog.setSucceed(r.getMessage());
        operationLog.setMessage("message");
        logService.save(operationLog);
    }
    /*修改操作*/
    @AfterReturning(value = "updateCell()", returning = "object")
    public void updateCell(JoinPoint joinPoint, Object object) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        //R r= (R) object;
        OperationLog operationLog=new OperationLog();
        operationLog.setLogType("业务日志");
        operationLog.setUserId(userid);
        operationLog.setLogName("修改操作");
        operationLog.setClassName(joinPoint.getTarget().getClass().getName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setCreateTime(new Date());
        //operationLog.setSucceed(r.getMessage());
        operationLog.setMessage("message");
        logService.save(operationLog);
    }
    /*分配角色*/
    @AfterReturning(value = "fenUserRole()", returning = "object")
    public void fenUserRole(JoinPoint joinPoint, Object object) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        //R r= (R) object;
        OperationLog operationLog=new OperationLog();
        operationLog.setLogType("业务日志");
        operationLog.setUserId(userid);
        operationLog.setLogName("分配角色操作");
        operationLog.setClassName(joinPoint.getTarget().getClass().getName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setCreateTime(new Date());
        //operationLog.setSucceed(r.getMessage());
        operationLog.setMessage("message");
        logService.save(operationLog);
    }

    /*分配权限*/
    @AfterReturning(value = "addRolePermission()", returning = "object")
    public void addRolePermission(JoinPoint joinPoint, Object object) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        //R r= (R) object;
        OperationLog operationLog=new OperationLog();
        operationLog.setLogType("业务日志");
        operationLog.setUserId(userid);
        operationLog.setLogName("分配权限操作");
        operationLog.setClassName(joinPoint.getTarget().getClass().getName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setCreateTime(new Date());
        //operationLog.setSucceed(r.getMessage());
        operationLog.setMessage("message");
        logService.save(operationLog);
    }


    @AfterThrowing(pointcut = "exceptionLogPoinCut()", throwing = "e")
    public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
        OperationLog operationLog=new OperationLog();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        operationLog.setLogType("异常日志");
        operationLog.setUserId(userid);
        operationLog.setLogName("异常");
        operationLog.setClassName(joinPoint.getTarget().getClass().getName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setCreateTime(new Date());
        operationLog.setMessage(e.getClass().getName()+ e.getMessage()+ e.getStackTrace());
        logService.save(operationLog);
    }




}
